package com.alingrad.android.froyoengine.util;

import com.alingrad.android.froyoengine.AllocationGuard;

public abstract class ObjectPool<E> extends AllocationGuard {
	protected FixedSizeArray<E> mObjects;
	
	public ObjectPool() {
		super();
		mObjects = new FixedSizeArray<E>();
		constructItems();
	}
	
	public ObjectPool(int capacity) {
		super();
		mObjects = new FixedSizeArray<E>(capacity);
		constructItems();
	}
	
	public E allocate() {
		return mObjects.removeLast();
	}
	
	public void release(E object) {
		mObjects.add(object);
	}
	
	private void constructItems() {
		for (int i = 0; i < mObjects.capacity(); i++) {
			mObjects.add(constructItem(i));
		}
	}
	
	public abstract E constructItem(int index);
}
